package com.ycfmedical.bed.ui.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bilibili.magicasakura.widgets.TintButton;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.ControlBedConstants;
import com.ycfmedical.bed.ControlConstants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.bean.BedParameterInfo;
import com.ycfmedical.bed.factory.OrderFactory;
import com.ycfmedical.bed.listener.IcontrolFreedom;
import com.ycfmedical.bed.present.ControlPresenter;
import com.ycfmedical.bed.utils.ResourceUtil;
import com.ycfmedical.bed.widgets.ControlSelectDialog;
import com.ycfmedical.bed.widgets.DialogIosAlert;
import com.ycfmedical.bed.wifi.WifiEvent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XFragment;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017-03-22. 体位控制_自由调整
 */

public class ControlFreedomFragment extends XFragment<ControlPresenter>
    implements View.OnTouchListener, View.OnClickListener, IcontrolFreedom
{
    @BindView(R.id.freedom_head_up)
    ImageView freedomHeadUp;
    
    @BindView(R.id.freedom_head_down)
    ImageView freedomHeadDown;
    
    @BindView(R.id.freedom_leg_up)
    ImageView freedomLegUp;
    
    @BindView(R.id.freedom_leg_down)
    ImageView freedomLegDown;
    
    @BindView(R.id.freedom_body_up)
    ImageView freedomBodyUp;
    
    @BindView(R.id.freedom_body_down)
    ImageView freedomBodyDown;
    
    @BindView(R.id.freedom_post_save)
    TintButton freedomPostSave;
    
    @BindView(R.id.freedom_post_resetting)
    TintButton freedomPostResetting;
    
    private AlertDialog dialog;
    
    String angleOrderMsg;
    
    public int getType()
    {
        return ControlConstants.CONTROL_FREEDOM;
    }
    
    @Override
    public void initData(Bundle savedInstanceState)
    {
        initOnTouchListener();
        _initBus();
    }
    
    private void initOnTouchListener()
    {
        freedomHeadUp.setOnTouchListener(this);
        freedomHeadDown.setOnTouchListener(this);
        freedomLegUp.setOnTouchListener(this);
        freedomLegDown.setOnTouchListener(this);
        freedomHeadUp.setOnTouchListener(this);
        freedomBodyUp.setOnTouchListener(this);
        freedomBodyDown.setOnTouchListener(this);
    }
    
    @Override
    public int getLayoutId()
    {
        
        return R.layout.fragment_control_freedom;
    }
    
    @Override
    public void onStart()
    {
        super.onStart();
    }
    
    @Override
    public ControlPresenter newP()
    {
        return new ControlPresenter();
    }
    
    public static ControlFreedomFragment newInstance()
    {
        return new ControlFreedomFragment();
    }
    
    private void _initBus()
    {
        BusProvider.getBus()
            .toObservableSticky(BedParameterInfo.class)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe((BedParameterInfo bedParameterInfo) -> {
                angleOrderMsg = bedParameterInfo.getAngleOrderMsg();
            });
    }
    
    // 保存体位
    @OnClick({R.id.freedom_post_save, R.id.freedom_post_resetting})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.freedom_post_save:
                WifiEvent wifi = WifiEvent.getInstance();
                wifi.senddata(OrderFactory.getMessage(ControlBedConstants.REQUEST_BED_ANGLE));
                new ControlSelectDialog(this).show(getFragmentManager(), "test-simple-calendar");
                break;
            case R.id.freedom_post_resetting:
                new DialogIosAlert(context).builder()
                    .setTitle("提示小Tips")
                    .setMsg("请问是否将所有体位设置为默认")
                    .setNegativeButton(ResourceUtil.getResources().getString(R.string.cancel), v -> {
                        getvDelegate().toastShort("你取消了操作！");
                    })
                    .setPositiveButton(ResourceUtil.getResources().getString(R.string.confirm), v -> {
                        resttingPost();
                    })
                    .show();
                break;
        }
    }
    
    // 发送指令
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            forbiddenButton();
            switch (v.getId())
            {
                case R.id.freedom_head_up:
                    getP().FreedomPost(1, true);
                    freedomHeadUp.setEnabled(true);
                    break;
                case R.id.freedom_head_down:
                    getP().FreedomPost(1, false);
                    freedomHeadDown.setEnabled(true);
                    break;
                case R.id.freedom_leg_up:
                    getP().FreedomPost(2, true);
                    freedomLegUp.setEnabled(true);
                    break;
                case R.id.freedom_leg_down:
                    getP().FreedomPost(2, false);
                    freedomLegDown.setEnabled(true);
                    break;
                case R.id.freedom_body_up:
                    getP().FreedomPost(3, true);
                    freedomBodyUp.setEnabled(true);
                    break;
                case R.id.freedom_body_down:
                    getP().FreedomPost(3, false);
                    freedomBodyDown.setEnabled(true);
                    break;
                case R.id.btn_save:
                    
                    break;
            }
        }
        // 发送调整完整指令
        else if (event.getAction() == MotionEvent.ACTION_UP)
        {
            ReStartButton();
            getP().FreedomFinish();
        }
        return false;
    }
    
    private void forbiddenButton()
    {
        // 禁止按钮点击状态
        freedomHeadUp.setEnabled(false);
        freedomHeadDown.setEnabled(false);
        freedomLegUp.setEnabled(false);
        freedomLegDown.setEnabled(false);
        freedomHeadUp.setEnabled(false);
        freedomBodyUp.setEnabled(false);
        freedomBodyDown.setEnabled(false);
    }
    
    private void ReStartButton()
    {
        // 重置按钮点击状态
        freedomHeadUp.setEnabled(true);
        freedomHeadDown.setEnabled(true);
        freedomLegUp.setEnabled(true);
        freedomLegDown.setEnabled(true);
        freedomHeadUp.setEnabled(true);
        freedomBodyUp.setEnabled(true);
        freedomBodyDown.setEnabled(true);
    }
    
    @Override
    public void setquicklyPostId(int num)
    {
        getP().savePost(angleOrderMsg, num);
        XLog.e("SJH", "dgsd;lgfkj;sgf");
    }

    //重置所有体位方法
    private void resttingPost()
    {
        getP().savePost(Constants.DEFAULT_POST_SLEEP, 1);
        getP().savePost(Constants.DEFAULT_POST_RELAXATION, 2);
        getP().savePost(Constants.DEFAULT_POST_REPAST, 3);
        getP().savePost(Constants.DEFAULT_POST_WORK, 4);
    }

}
